﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RayDen.Library.Core.Primitives;

namespace RayDen.Library.Data.Scene.SCFF
{
    public static class ValueReader
    {
        static readonly Dictionary<Type, Func<string, object>> converters = new Dictionary<Type, Func<string, object>>()
            {
                { typeof(Vector), (v=> Vector.FromString(v))},

                {
                    typeof(Normal), (v=>new Normal(Vector.FromString(v)))
                },

                {
                    typeof(Point), (v=>new Point(Vector.FromString(v)))
                },

                {
                    typeof(RgbSpectrum), (v=>RgbSpectrum.FromStringData(v))
                },

                {
                    typeof(float), (v=>float.Parse(v, NumberStyles.Any, CultureInfo.InvariantCulture))
                },

                {
                    typeof(int), (v=>int.Parse(v, NumberStyles.Any, CultureInfo.InvariantCulture))
                },
                {
                    typeof(bool), (v=>bool.Parse(v))
                },
            };


        public static TValue Convert<TValue>(string data)
        {
            return (TValue)converters[typeof(TValue)](data.Trim(' ', '"', '\r', '\t', '\n'));
        }

        private static TValue Convert<TValue>(string data, TValue @default)
        {
            try
            {
                return (TValue)converters[typeof(TValue)](data);
            }

            catch
            {
                return @default;
            }
        }

        public static TValue Convert<TValue>(object data, TValue @default)
        {
            try
            {
                return (TValue)converters[typeof(TValue)](data.ToString());
            }

            catch
            {
                return @default;
            }
        }
        public static string NormalizeString(this string readedstring)
        {
            return readedstring.Trim('"', ' ');
        }

    }
}
